package com.cardone.platform.configuration.dao;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.configuration.dto.DictionaryDto;
import com.google.common.collect.Maps;

/**
 * 字典
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class DictionaryJdbcDao implements DictionaryDao {
	@Override
	public int deleteByIds(final DictionaryDto deleteDictionary) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteDictionary.getAttrs())) {
			model.putAll(deleteDictionary.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteDictionary.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final DictionaryDto findDictionary) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findDictionary, Attributes.code.name(), Attributes.typeId.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findDictionary, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findDictionary.getAttrs())) {
			model.putAll(findDictionary.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final DictionaryDto findDictionary) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findDictionary);

		final Model model = ModelUtils.newModel(new ModelArgs(findDictionary, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findDictionary.getAttrs())) {
			model.putAll(findDictionary.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> P findByTypeCodeAndCode(final Class<P> mappedClass, final String typeCode, final String code) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.typeCode.name(), typeCode);
		model.put(Attributes.code.name(), code);

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByTypeCodeAndCode.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final DictionaryDto findListDictionary) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListDictionary).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListDictionary.getAttrs())) {
			model.putAll(findListDictionary.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public <P> List<P> findListByTypeCode(final Class<P> mappedClass, final String typeCode) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.typeCode.name(), typeCode);

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByTypeCode.id(), model);
	}

	@Override
	public <P> List<P> findListByTypeId(final Class<P> mappedClass, final String typeId) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.typeId.name(), typeId);

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByTypeId.id(), model);
	}

	@Override
	public int insertByCode(final DictionaryDto insertDictionary) {
		if (StringUtils.isBlank(insertDictionary.getId())) {
			insertDictionary.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertDictionary));

		if (MapUtils.isNotEmpty(insertDictionary.getAttrs())) {
			model.putAll(insertDictionary.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final DictionaryDto paginationDictionary) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationDictionary).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationDictionary.getAttrs())) {
			model.putAll(paginationDictionary.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationDictionary), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final DictionaryDto readDictionary) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readDictionary, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readDictionary.getAttrs())) {
			model.putAll(readDictionary.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readDictionary);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readDictionary, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final DictionaryDto saveDictionary) {
		final P oldDictionary = this.findByCodeOrId(mappedClass, saveDictionary);

		if (oldDictionary == null) {
			this.insertByCode(saveDictionary);

			return this.findByCodeOrId(mappedClass, saveDictionary);
		}

		final int updateCount = this.updateByCode(saveDictionary, oldDictionary);

		if (updateCount < 1) {
			return oldDictionary;
		}

		return this.findByCodeOrId(mappedClass, saveDictionary);
	}

	@Override
	public int updateByCode(final DictionaryDto updateDictionary) {
		final DictionaryDto oldDictionary = this.findByCodeOrId(DictionaryDto.class, updateDictionary);

		return this.updateByCode(updateDictionary, oldDictionary);
	}

	private <P> int updateByCode(final DictionaryDto updateDictionary, final P oldDictionary) {
		if (oldDictionary == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.orderNum.name(), Attributes.remark.name(), DictionaryDto.Attributes.explain.name() }, updateDictionary);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateDictionary, oldDictionary, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldDictionary, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldDictionary, updateDictionary, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateDictionary, updateProperties), new ModelArgs(updateDictionary, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateDictionary.getAttrs())) {
			model.putAll(updateDictionary.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}